package clueless.server;

import java.util.ArrayList;

import clueless.utilities.Card;
import clueless.utilities.Character;
import clueless.utilities.Connection;
import clueless.utilities.Map;
import clueless.utilities.Room;

public class Player {

	private String     			m_character;
	private Connection 			m_connection;
	private Room      			m_room;
	private boolean    			m_isStillPlaying;
	private ArrayList<Card>    	m_cards;
	
	public Player(String character, Connection connection) {

		m_character      = character;
		m_connection     = connection;
		//m_room           = determineStartingRoom(character);
		m_isStillPlaying = true;
		m_cards = new ArrayList<Card>();
	}

	public void setStartingRoom(Map gameMap) {
		if(m_character.equals(Character.m_COL_MUSTARD )) m_room = gameMap.getLounge().getDownRoom();
		if(m_character.equals(Character.m_MISS_SCARLET)) m_room = gameMap.getLounge().getLeftRoom();
		if(m_character.equals(Character.m_MR_GREEN    )) m_room = gameMap.getConservatory().getRightRoom();
		if(m_character.equals(Character.m_MRS_PEACOCK )) m_room = gameMap.getConservatory().getUpRoom();
		if(m_character.equals(Character.m_MRS_WHITE   )) m_room = gameMap.getKitchen().getLeftRoom();
		if(m_character.equals(Character.m_PROF_PLUM   )) m_room = gameMap.getStudy().getDownRoom();
	}
	
	public String getCharacter() {
		return m_character;
	}
	
	public Connection getConnection() {
		return m_connection;
	}

	public void setRoom(String room, Map map) {
		;
		if (room.equalsIgnoreCase(Room.m_BALLROOM)) {
			m_room = map.getBallroom();
		} else if (room.equalsIgnoreCase(Room.m_BILLIARD_ROOM)) {
			m_room = map.getBillardRoom();
		} else if (room.equalsIgnoreCase(Room.m_CONSERVATORY)) {
			m_room = map.getConservatory();
		} else if (room.equalsIgnoreCase(Room.m_DINING_ROOM)) {
			m_room = map.getDiningRoom();
		} else if (room.equalsIgnoreCase(Room.m_HALL)) {
			m_room = map.getHall();
		} else if (room.equalsIgnoreCase(Room.m_KITCHEN)) {
			m_room = map.getKitchen();
		} else if (room.equalsIgnoreCase(Room.m_LIBRARY)) {
			m_room = map.getLibrary();
		} else if (room.equalsIgnoreCase(Room.m_LOUNGE)) {
			m_room = map.getLounge();
		} else if (room.equalsIgnoreCase(Room.m_STUDY)) {
			m_room = map.getStudy();
		}
	}

	public Room getRoom() {
		return m_room;
	}
	
	public void setIsStillPlaying(boolean isStillPlaying) {
		m_isStillPlaying = isStillPlaying;
	}

	public boolean getIsStillPlaying() {
		return m_isStillPlaying;
	}
	
	public String getPossibleMoves(ArrayList<Player> players) {
		String moves = "";
		
		boolean valid = true;
		if (m_room.getUpRoom() == null) valid = false;
		else if (m_room.getUpRoom().getMyRoom().toLowerCase().contains("hallway") != false) {
			for (int i = 0; i < players.size(); i++) {
				if ((m_character.equals(players.get(i).getCharacter()))
						|| (players.get(i).getIsStillPlaying() == false)) continue;
				if (m_room.getUpRoom().getMyRoom().equals(players.get(i).getRoom().getMyRoom())) {
					valid = false;
					break;
				}
			}
		}
		if (valid == true) moves += "Up,";
		
		valid = true;
		if (m_room.getDownRoom() == null) valid = false;
		else if (m_room.getDownRoom().getMyRoom().toLowerCase().contains("hallway") != false) {
			for (int i = 0; i < players.size(); i++) {
				if ((m_character.equals(players.get(i).getCharacter()))
					|| (players.get(i).getIsStillPlaying() == false)) continue;
				if (m_room.getDownRoom().getMyRoom().equals(players.get(i).getRoom().getMyRoom())) {
					valid = false;
					break;
				}
			}
		}
		if (valid == true) moves += "Down,";
		
		valid = true;
		if (m_room.getLeftRoom() == null) valid = false;
		else if (m_room.getLeftRoom().getMyRoom().toLowerCase().contains("hallway") != false) {
			for (int i = 0; i < players.size(); i++) {
				if ((m_character.equals(players.get(i).getCharacter()))
						|| (players.get(i).getIsStillPlaying() == false)) continue;
				if (m_room.getLeftRoom().getMyRoom().equals(players.get(i).getRoom().getMyRoom())) {
					valid = false;
					break;
				}
			}
		}
		if (valid == true) moves += "Left,";
		
		valid = true;
		if (m_room.getRightRoom() == null) valid = false;
		else if (m_room.getRightRoom().getMyRoom().toLowerCase().contains("hallway") != false) {
			for (int i = 0; i < players.size(); i++) {
				if ((m_character.equals(players.get(i).getCharacter()))
						|| (players.get(i).getIsStillPlaying() == false)) continue;
				if (m_room.getRightRoom().getMyRoom().equals(players.get(i).getRoom().getMyRoom())) {
					valid = false;
					break;
				}
			}
		}
		if (valid == true) moves += "Right,";
		
		if (m_room.getSecretRoom() != null) moves += "Secret,";

		
//		if (m_room.getUpRoom() != null) moves += "Up,";
//		if (m_room.getDownRoom() != null) moves += "Down,";
//		if (m_room.getLeftRoom() != null) moves += "Left,";
//		if (m_room.getRightRoom() != null) moves += "Right,";
//		if (m_room.getSecretRoom() != null) moves += "Secret,";
		
		return moves;
	}
	
	public boolean move(String motion) {
		switch (motion.toLowerCase()) {
		case "up":
			if (m_room.getUpRoom() != null) {
				m_room = m_room.getUpRoom();
				return true;
			}
			break;
		case "down":
			if (m_room.getDownRoom() != null) {
				m_room = m_room.getDownRoom();
				return true;
			}
			break;
		case "left":
			if (m_room.getLeftRoom() != null) {
				m_room = m_room.getLeftRoom();
				return true;
			}
			break;
		case "right":
			if (m_room.getRightRoom() != null) {
				m_room = m_room.getRightRoom();
				return true;
			}
			break;
		case "secret":
			if (m_room.getSecretRoom() != null) {
				m_room = m_room.getSecretRoom();
				return true;
			}
			break;
		}
		return false;
	}
	
	public boolean inRoom()
	{
		switch (m_room.getMyRoom()) {
		case Room.m_BALLROOM :
		case Room.m_BILLIARD_ROOM :
		case Room.m_CONSERVATORY :
		case Room.m_DINING_ROOM :
		case Room.m_HALL :
		case Room.m_KITCHEN :
		case Room.m_LIBRARY :
		case Room.m_LOUNGE :
		case Room.m_STUDY : return true;
		}
		return false;
	}
	
	public String getCards() {
		String cardString = "";
		for (Card card : m_cards) {
			cardString += card.getValue().toLowerCase() + ",";
		}
		return cardString;
	}
	
	public boolean haveCard(String cardValue) {
		for (Card card : m_cards) {
			if (card.getValue().equalsIgnoreCase(cardValue)) return true;
		}
		return false;
	}
	
	public void addCard(Card card) {
		m_cards.add(card);
	}
	
	public int numCards() {
		return m_cards.size();
	}

}
